package com.recSys.dataTraining;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.recSys.util.ResourcePathHandler;

import de.bwaldvogel.liblinear.Feature;
import de.bwaldvogel.liblinear.FeatureNode;
import de.bwaldvogel.liblinear.Linear;
import de.bwaldvogel.liblinear.Model;

/**
 * Created by dell on 2018/1/21.
 */
public class Train4RecFun {

    static final String rootPath = ResourcePathHandler.getProgrameRootPath();
    static final String toolPath = rootPath + "\\tools\\train.exe";
    static final String filePath4Fun_train = rootPath + "\\data\\format\\recFun_format.txt";
    static final String filePath4Fun_model = rootPath + "\\data\\model\\recFun_model.txt";
    
    public static Model modelForRecFun;

    public static void trainL2L2D () {
        try {
            Runtime rt = Runtime.getRuntime();
            System.out.println(rootPath);
            Process pr = rt.exec(toolPath + " -s 1 " + filePath4Fun_train + " " + filePath4Fun_model);
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line = null;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setModel() {
    	try {
    		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Fun_model))));
            modelForRecFun = Linear.loadModel(br);
            br.close();
    	} catch (IOException e) {
            e.printStackTrace();
        }
    	
    }
    
    public static void main(String[] args) {
        try {
            trainL2L2D();
            Feature[] instance = { new FeatureNode(679, 1), new FeatureNode(1773, 1) };
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Fun_model))));
            Model model = Linear.loadModel(br);
            br.close();
            double prediction = Linear.predict(model, instance);
            System.out.println(prediction);
        } catch (Exception e) {

        }
    }

}
